<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        window.onload = function (){
            var box1 = document.getElementsByClassName("box1")[0];
            // 给box1绑定鼠标按下时的响应事件
            box1.onmousedown = function(event){
                // 给整个页面绑定鼠标移动的响应事件
                // 求div的偏移量
                var ol = event.clientX - box1.offsetLeft;
                var ot = event.clientY - box1.offsetTop;
                document.onmousemove = function(event){
                    var x = event.clientX - ol;
                    var y = event.clientY - ot;
                    // 修改box1中的样式
                    box1.style.left = x+"px";
                    box1.style.top = y+"px";
                }
                // 给整个页面绑定鼠标松开的响应事件
                document.onmouseup = function(){
                    // 关闭鼠标移动的响应事件
                    document.onmousemove = null;
                    // 关闭鼠标松开的响应事件
                    document.onmouseup = null;
                }
            }
        }
    </script>
    <style>
        .box1{
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: orange;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
</body>
</html>